package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.service.ReportService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @version V1.0
 * @author: luoqianjin
 * @date: 2019/8/7 18:35
 * @description: 统计报表
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    /**
     * 获得运营统计数据
     * Map数据格式：
     *      todayNewMember -> number
     *      totalMember -> number
     *      thisWeekNewMember -> number
     *      thisMonthNewMember -> number
     *      todayOrderNumber -> number
     *      todayVisitsNumber -> number
     *      thisWeekOrderNumber -> number
     *      thisWeekVisitsNumber -> number
     *      thisMonthOrderNumber -> number
     *      thisMonthVisitsNumber -> number
     *      hotSetmeal -> List<Setmeal>
     */
    @Override
    public Map<String, Object> getBusinessReport() {
        Map<String, Object> map = new HashMap<>();

        // 0.准备日期字符串（当前日期，本周一对应的日期，本月一号对应的日期）
        LocalDate now = LocalDate.now();
        String today = getDateString(now);
        String monday =getDateString(now.with(DayOfWeek.MONDAY)) ;
        String monthOne = getDateString(now.with(TemporalAdjusters.firstDayOfMonth()));

        //  1. 获取会员数据
      Integer todayNewMember =  memberDao.findMemberCountByDate(today);
        Integer totalMember  = memberDao.findMemberTotalCount();
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(monthOne);

        //  2. 获取预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(monday);
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(monthOne);
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(monthOne);

        //  3. 获取热门套餐
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        map.put("reportDate",today);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }


    /**
     *
     * @param now
     * @return 抽取日期字符串方法/获取日期字符串
     *
     */
    private String getDateString(LocalDate now) {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now);
    }
}
